/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - graphics/shaders
// Copyright( c) 2015.  All Rights Reserved
//
// File:		AEShaderConstantBuffer_ogl.cpp
// Author:		Gianluca Belardelli
// Date:		05/02/2015
//
/////////////////////////////////////////////////////////////////////////
#include <AEEngine.h>

GLint	AEShaderConstantBuffer::m_nMaxBindings = -1;
GLint	AEShaderConstantBuffer::m_nMaxBlockSize = -1;
GLint	AEShaderConstantBuffer::m_nMaxVertexBlocks = -1;
GLint	AEShaderConstantBuffer::m_nMaxFragmentBlocks = -1;

#if defined (AE_OPENGL)

GLint	AEShaderConstantBuffer::m_nMaxGeometryBlocks = -1;

#endif

void AEShaderConstantBuffer::_platformInit( void )
{
	glGenBuffers( 1, &m_uiHandle );
	AEASSERT( m_uiHandle != GL_INVALID_VALUE );

	if( m_nMaxBindings != -1 )
		return;

#if defined (AE_OPENGL)
	glGetIntegerv( GL_MAX_UNIFORM_BUFFER_BINDINGS, &m_nMaxBindings );
	glGetIntegerv( GL_MAX_UNIFORM_BLOCK_SIZE, &m_nMaxBlockSize );
	glGetIntegerv( GL_MAX_VERTEX_UNIFORM_BLOCKS, &m_nMaxVertexBlocks );
	glGetIntegerv( GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &m_nMaxFragmentBlocks );
	glGetIntegerv( GL_MAX_GEOMETRY_UNIFORM_BLOCKS, &m_nMaxGeometryBlocks );
#endif
}

AEUINT32 AEShaderConstantBuffer::AllocateBuffer( AEINT32 nFirstReg, AEINT32 nEntryCount, void *lpCustomMem )
{
	if( m_uiHandle == GL_INVALID_VALUE )
		return AEFALSE;

	m_spTable->AllocateEntries( nEntryCount );
#ifdef AE_OPENGL
	glBindBuffer( GL_UNIFORM_BUFFER, m_uiHandle );
	glBufferData( GL_UNIFORM_BUFFER, nEntryCount, lpCustomMem, GL_DYNAMIC_DRAW );
	glBindBuffer( GL_UNIFORM_BUFFER, 0 );
#endif
	return AETRUE;
}

AEUINT32 AEShaderConstantBuffer::BindConstantTable( AEShaderConstantTable *lpTable )
{
	if( m_uiHandle == GL_INVALID_VALUE )
		return AEFALSE;
#ifdef AE_OPENGL
	glBindBuffer( GL_UNIFORM_BUFFER, m_uiHandle );
#endif
}
